// pages/booksdetail_third/booksdetail_third.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startdate: '2021-01-11',
    enddate: '2021-01-12',
    totaldays: null
  },
  // 开始时间选择事件
  startbindDateChange: function (e) {
    this.setData({
      startdate: e.detail.value
    })
    var start = (new Date((this.data.startdate.replace(/-/g, "/"))));
    var end = (new Date((this.data.enddate.replace(/-/g, "/"))));
    var days = parseInt((end.getTime() - start.getTime()) / (1000 * 60 * 60 * 24));
    if (days <= 0) {
      wx.showToast({
        title: '请选择正确的时间',
      })
      this.setData({
        startdate: '2021-1-11',
        enddate: '2021-1-12'
      })
      this.totaldays();
    }
    this.totaldays();
  },
  // 结束时间选择事件
  endbindDateChange: function (e) {
    this.setData({
      enddate: e.detail.value
    })
    var start = (new Date((this.data.startdate.replace(/-/g, "/"))));
    var end = (new Date((this.data.enddate.replace(/-/g, "/"))));
    var days = parseInt((end.getTime() - start.getTime()) / (1000 * 60 * 60 * 24));

    if (days <= 0) {
      wx.showToast({
        title: '请选择正确的时间',
      })
      this.setData({
        startdate: '2021-1-11',
        enddate: '2021-1-12'
      })
      this.totaldays();
    }
    this.totaldays();
  },
  // 差值天数事件
  totaldays() {
    var start = (new Date((this.data.startdate.replace(/-/g, "/"))));
    var end = (new Date((this.data.enddate.replace(/-/g, "/"))));
    var days = parseInt((end.getTime() - start.getTime()) / (1000 * 60 * 60 * 24));
    this.setData({
      totaldays: days
    })
  },
  // 还书时间比借书时间早,报警事件
  warn() {
    if (this.data.totaldays < 0) {
      wx.showModal({
        title: '选择日期错误,请重新选择',
        showCancel: false
      });
      this.data.startdate = '',
        this.data.enddate = '',
        console.log(this.data.startdate);
    }
  },
  // 底部按钮申请延期还书事件
  applydelay() {
    wx.navigateTo({
      url: '/pages/applydelay/applydelay',
    })
  },
  // 页面加载事件
  onLoad: function (options) {
    this.totaldays();
  },
})